import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

/**
 * A JDialog that holds the profile panels.
 *
 * @author Jacel Evasco
 * @author Roselle Nympha Lim
 * @version 1.0
 */
public class ProfileMenu extends JDialog {

    private JPanel tabs = new JPanel();
    private JLabel tabsBG = new JLabel(new ImageIcon(
        "images\\backgrounds\\profileTabsBG.png"));
    private static JPanel cards = new JPanel();
    private JLabel cardsBG = new JLabel(new ImageIcon(
        "images\\backgrounds\\profileCardsBG.png"));
    private JButton add = new JButton(new ImageIcon(
        "images\\icons\\profileAddNormal.png"));
    private JButton login = new JButton(new ImageIcon(
        "images\\icons\\profileLoginNormal.png"));
    private JButton edit = new JButton(new ImageIcon(
        "images\\icons\\profileEditNormal.png"));
    private ProfileMaker profileMaker = new ProfileMaker();
    private ProfileAuthenticator profileAuthenticator = new ProfileAuthenticator();
    private ProfilePeeker profilePeeker = new ProfilePeeker();
    private ProfileEditor profileEditor = new ProfileEditor();

    Connection connection = null;
    ResultSet resultSet = null;
    PreparedStatement preparedStatement = null;

    public ProfileMenu() {
        cleanUp();
        assemble();
        configure();
        design();
        setTriggers();
        setVisible(false);
    }// end of ProfileMenu()

    private void cleanUp() {
        profileAuthenticator.setVisible(false);
        profileMaker.setVisible(false);
        profileEditor.setVisible(false);
    } // end of cleanUp method

    private void prepareConnection() {
        try {
            connection = DatabaseUtility.prepareConnection();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    } // end of prepareConnection method

    private void assemble() {
        // tabs
        tabs.setLayout(new BoxLayout(tabs, BoxLayout.X_AXIS));
        tabs.add(Box.createRigidArea(new Dimension(185, 0)));
        tabs.add(add);
        tabs.add(Box.createRigidArea(new Dimension(15, 0)));
        tabs.add(login);
        tabs.add(Box.createRigidArea(new Dimension(15, 0)));
        tabs.add(edit);
        // cards
        cards.setLayout(new CardLayout());
        cards.add(profileAuthenticator, "profileAuthenticator");
        cards.add(profileMaker, "profileMaker");
        cards.add(profilePeeker, "profilePeeker");
        cards.add(profileEditor, "profileEditor");
        // tabsBG
        tabsBG.setLayout(new BorderLayout());
        tabsBG.add(tabs);
        // cardsBG
        cardsBG.setLayout(new BorderLayout());
        cardsBG.add(cards);
        // content pane
        getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        getContentPane().add(tabsBG);
        getContentPane().add(cardsBG);
        setTitle("Profile Menu > Login");
        setSize(500, 330);
        setLocationRelativeTo(null);
        setResizable(false);
    }// end of assemble()

    private void configure() {
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        edit.setEnabled(false);
        edit.setVisible(false);
    }// end of configure()

    private void design() {
        // sizes
        tabs.setPreferredSize(new Dimension(500, 46));
        tabs.setMinimumSize(new Dimension(500, 46));
        tabs.setMaximumSize(new Dimension(500, 46));
        add.setPreferredSize(new Dimension(32, 46));
        add.setMinimumSize(new Dimension(32, 46));
        add.setMaximumSize(new Dimension(32, 46));
        login.setPreferredSize(new Dimension(32, 46));
        login.setMinimumSize(new Dimension(32, 46));
        login.setMaximumSize(new Dimension(32, 46));
        edit.setPreferredSize(new Dimension(32, 46));
        edit.setMinimumSize(new Dimension(32, 46));
        edit.setMaximumSize(new Dimension(32, 46));
        // backgrounds
        tabs.setOpaque(false);
        cards.setOpaque(false);
        profileMaker.setOpaque(false);
        profileAuthenticator.setOpaque(false);
        profilePeeker.setOpaque(false);
        profileEditor.setOpaque(false);
        // button styles
        add.setOpaque(false);
        add.setContentAreaFilled(false);
        add.setBorderPainted(false);
        login.setOpaque(false);
        login.setContentAreaFilled(false);
        login.setBorderPainted(false);
        edit.setOpaque(false);
        edit.setContentAreaFilled(false);
        edit.setBorderPainted(false);
    }// end of design()

    private void setTriggers() {

        ActionListener actionTrigger = new ActionListener() {
            public void actionPerformed(ActionEvent anActionEvent) {
                if (anActionEvent.getSource() == add) {
                    ((CardLayout) cards.getLayout()).show(cards,
                        "profileMaker");
                    setTitle("Profile Menu > New");

                    profileAuthenticator.setVisible(false);
                    profileEditor.setVisible(false);
                } else if (anActionEvent.getSource() == login) {
                    loginOrLogout();

                    profileMaker.setVisible(false);
                    profileEditor.setVisible(false);
                } else if (anActionEvent.getSource() == edit) {
                    ((CardLayout) cards.getLayout()).show(cards,
                        "profileEditor");
                    setTitle("Profile Menu > Edit");

                    profileMaker.setVisible(false);
                    profileAuthenticator.setVisible(false);
                }
            }// end of actionPerformed()
        };// end of actionTrigger

        MouseListener viewerTrigger = new MouseListener() {
            public void mouseClicked(MouseEvent me) {
            } // end of mouseClicked()

            public void mouseEntered(MouseEvent me) {
                if (me.getSource() == add) {
                    add.setIcon(new ImageIcon(
                        "images\\icons\\profileAddEntered.png"));
                }
                if (me.getSource() == login) {
                    login.setIcon(new ImageIcon(
                        "images\\icons\\profileLoginEntered.png"));
                }
                if (me.getSource() == edit) {
                    edit.setIcon(new ImageIcon(
                        "images\\icons\\profileEditEntered.png"));
                }
            } // end of mouseEntered()

            public void mouseExited(MouseEvent me) {
                if (me.getSource() == add) {
                    add.setIcon(new ImageIcon(
                        "images\\icons\\profileAddNormal.png"));
                }
                if (me.getSource() == login) {
                    login.setIcon(new ImageIcon(
                        "images\\icons\\profileLoginNormal.png"));
                }
                if (me.getSource() == edit) {
                    edit.setIcon(new ImageIcon(
                        "images\\icons\\profileEditNormal.png"));
                }
            } // end of mouseExited()

            public void mousePressed(MouseEvent me) {
                if (me.getSource() == add) {
                    add.setIcon(new ImageIcon(
                        "images\\icons\\profileAddPressed.png"));
                }
                if (me.getSource() == login) {
                    login.setIcon(new ImageIcon(
                        "images\\icons\\profileLoginPressed.png"));
                }
                if (me.getSource() == edit) {
                    edit.setIcon(new ImageIcon(
                        "images\\icons\\profileEditPressed.png"));
                }
            } // end of mousePressed()

            public void mouseReleased(MouseEvent me) {
                if (me.getSource() == add) {
                    add.setIcon(new ImageIcon(
                        "images\\icons\\profileAddNormal.png"));
                }
                if (me.getSource() == login) {
                    login.setIcon(new ImageIcon(
                        "images\\icons\\profileLoginNormal.png"));
                }
                if (me.getSource() == edit) {
                    edit.setIcon(new ImageIcon(
                        "images\\icons\\profileEditNormal.png"));
                }
            }
        };

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                try {
                    DatabaseUtility.prepareConnection();
                } catch (Exception noise) {
                    noise.printStackTrace();
                }
                try {
                    String[][] temp = DatabaseUtility
                        .getResults("SELECT student.studentID "
                            + "FROM student "
                            + "WHERE student.isLoggedIn = 1");
                    if (temp == null || temp.length == 0) {
                        int selection = JOptionPane
                            .showConfirmDialog(
                                null,
                                "You are not logged in. Hitting Yes will close "
                                    + "the application. Are you sure you want to exit?",
                                "Confirm operation",
                                JOptionPane.YES_NO_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        switch (selection) {
                        case 0:
                            System.exit(0);
                            break;
                        }
                    } else {
                        setDefaultCloseOperation(HIDE_ON_CLOSE);
                    }
                } catch (Exception noise) {
                    noise.printStackTrace();
                }
            }
        });

        add.addMouseListener(viewerTrigger);
        add.addActionListener(actionTrigger);
        login.addMouseListener(viewerTrigger);
        login.addActionListener(actionTrigger);
        edit.addMouseListener(viewerTrigger);
        edit.addActionListener(actionTrigger);

    }// end of setTriggers

    public void loginOrLogout() {
        prepareConnection();

        String query = "SELECT * FROM requirementstrackerschema.student"
            + " WHERE isLoggedIn = '1'";

        try {
            preparedStatement = connection.prepareStatement(query);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) { // profilePeeker
                ((CardLayout) cards.getLayout()).show(cards,
                    "profilePeeker");
                setTitle("Profile Menu > Logout");
            } else { // profileAuthenticator
                ((CardLayout) cards.getLayout()).show(cards,
                    "profileAuthenticator");
                setTitle("Profile Menu > Login");
            } // end of if-else
        } catch (Exception e) {
            e.printStackTrace();
        } // end of try-catch

    } // end of loginOrLogout method

    public static JPanel getCards() {
        return cards;
    }// end of getCards()

    public ProfileEditor getProfileEditor() {
        return profileEditor;
    }// end of getProfileEditor()

    public ProfilePeeker getProfilePeeker() {
        return profilePeeker;
    }// end of getProfilePeeker()

    public ProfileAuthenticator getProfileAuthenticator() {
        return profileAuthenticator;
    }// end of getProfileAuthenticator()

    public ProfileMaker getProfileMaker() {
        return profileMaker;
    }// end of getProfileMaker()

    public JButton getEdit() {
        return edit;
    }// end of getEdit
}// end of ProfileMenu